package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.pm.dto.DocumentInformationListParamDTO;
import com.oa.pm.dto.DocumentInformationSaveDTO;
import com.oa.pm.entity.DocumentInformation;
import com.oa.pm.service.IDocumentInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

import javax.validation.Valid;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/documentInformation")
@Api(description = "文档信息(培训资料)表模块")
public class DocumentInformationController extends BaseController {

    private final IDocumentInformationService documentInformationService;

    @Autowired
    public DocumentInformationController(IDocumentInformationService documentInformationService) {
        this.documentInformationService = documentInformationService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @PostMapping("/findList")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp<List<DocumentInformation>> findList(DocumentInformationListParamDTO documentInformationListParamDTO) {
        return new Resp().success(documentInformationService.findDocumentInformationList(documentInformationListParamDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @PostMapping("/findById")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<DocumentInformation> findById(@ApiParam(value = "id", required = true) @RequestParam String id) {
        return new Resp().success(documentInformationService.findDocumentInformationById(id));
    }


    /**
     * 新增
     *
     * @return Resp
     */
    @PostMapping("/save")
    @CommonController
    @Description("保存")
    @ApiOperation(value = "保存")
    public Resp add(@ApiParam(value = "文档信息(培训资料)表对象")@RequestBody DocumentInformationSaveDTO documentInformationSaveDTO) {
        DocumentInformation documentInformation = new DocumentInformation();
        BeanUtils.copyProperties(documentInformationSaveDTO, documentInformation);
        documentInformation.setPromulgator(getLoginUserId());
        documentInformation.setReleaseTime(new Date());
        documentInformation.setDownloadCount(0);
        if (documentInformationService.insertOrUpdate(documentInformation)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        DocumentInformation documentInformation = documentInformationService.selectById(id);
        if (documentInformation != null && documentInformation.getIsDeleted() == 2) {
            documentInformation.setIsDeleted(1);
            if (documentInformationService.updateById(documentInformation)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
